﻿#region Using Directives

using System.IO;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Psi.Tree;

#endregion

namespace CCN.StyleCopPlugin
{
    /// <summary>
    /// Manages the temporary file the current in memory 
    /// source code is saved to for processing
    /// </summary>
    public class TempFileManager
    {
        private string _tempFileName;

        /// <summary>
        /// Creates the temp file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns></returns>
        public string CreateTempFile(IFile file)
        {
            var csharpSourceFile = file.ProjectFile;
            var filePath = GetPath(csharpSourceFile);
            _tempFileName = GetTempFilePath(filePath);
            CreateTempFile(_tempFileName, file.GetText());

            return _tempFileName;
        }

        /// <summary>
        /// Gets the path.
        /// </summary>
        /// <param name="projectFile">The project file.</param>
        /// <returns></returns>
        private static string GetPath(IProjectItem projectFile)
        {
            var filePath = projectFile.Location.FullPath;
            return filePath;
        }

        /// <summary>
        /// Deletes the temp file.
        /// </summary>
        public void DeleteTempFile()
        {
            if (!string.IsNullOrEmpty(_tempFileName))
            {
                DeleteTempFile(_tempFileName);
            }
        }

        /// <summary>
        /// Gets the temp file path.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        private static string GetTempFilePath(string filePath)
        {
            var tempFileName = GetTempFileName(filePath);
            var tempDirectory = GetTempDirectory(filePath);
            var tempFilePath = Path.Combine(tempDirectory, tempFileName);
            return tempFilePath;
        }

        private static string GetTempDirectory(string filePath)
        {
            var directory = Path.GetDirectoryName(filePath);
            Path.Combine(directory, "_ReSharper.StyleCop");
            return directory;
        }

        private static string GetTempFileName(string filePath)
        {
            var fileExtra = Path.GetTempFileName();
            fileExtra = Path.GetFileNameWithoutExtension(fileExtra);

            var extension = Path.GetExtension(filePath);
            var fileName = Path.GetFileNameWithoutExtension(filePath);

            var tempFileName = fileName + "_" + fileExtra + extension;

            return tempFileName;
        }

        /// <summary>
        /// Deletes the temp file.
        /// </summary>
        /// <param name="tempFilePath">The temp file path.</param>
        private static void DeleteTempFile(string tempFilePath)
        {
            if (File.Exists(tempFilePath))
            {
                File.Delete(tempFilePath);
            }
        }

        /// <summary>
        /// Creates the temp file.
        /// </summary>
        /// <param name="tempFilePath">The temp file path.</param>
        /// <param name="fileContents">The file contents.</param>
        private static void CreateTempFile(string tempFilePath, string fileContents)
        {
            using (var streamWriter = new StreamWriter(tempFilePath))
            {
                streamWriter.Write(fileContents);
            }
        }
    }
}